package com.itkjb.leisurely.resource.sys.support;

import com.itkjb.leisurely.authentication.core.common.support.RoleAndPermissionAndDevQueryEngine;
import com.itkjb.leisurely.resource.sys.model.SysDepartmentModel;
import com.itkjb.leisurely.resource.sys.model.SysMenuModel;
import com.itkjb.leisurely.resource.sys.model.SysRoleModel;
import com.itkjb.leisurely.resource.sys.service.ISysDepartmentService;
import com.itkjb.leisurely.resource.sys.service.ISysMenuService;
import com.itkjb.leisurely.resource.sys.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Copyright (C), 2021-2099
 *
 * @Author: lix
 * @Date: 2021/4/3 10:20 上午
 * @Description: 用户权限、角色、组织等获取引擎
 * @Version: V1.0.0
 */
@Component
public class RoleAndPermissionAndDevQueryEngineImpl implements RoleAndPermissionAndDevQueryEngine {

    @Autowired
    ISysRoleService sysRoleService;

    @Autowired
    ISysMenuService sysMenuService;

    @Autowired
    ISysDepartmentService departmentService;

    @Override
    public Map<String, String> queryRoles(String userid) {
        List<SysRoleModel> temRes = this.sysRoleService.findRoleIdAndCodeByUserId(userid);
        Map<String, String> res = null;
        if (temRes != null){
            res  = new HashMap<>(temRes.size());
            for(SysRoleModel roleModel:temRes){
                res.put(roleModel.getId(), "ROLE_"+roleModel.getCode());
            }
        }
        return res;
    }

    @Override
    public Map<String, String> queryPermission(String userId) {
        List<SysMenuModel> temRes = this.sysMenuService.findMenuPermissionByUserId(userId);
        Map<String, String> res = null;
        if(temRes !=null){
            res  =new HashMap<>(temRes.size());
            int i=0;
            for (SysMenuModel temRe : temRes) {
                res.put(i+"",temRe.getPermission());
                i++;
            }
        }
        return res;
    }

    @Override
    public Map<String, String> queryDeps(String userid) {
        List<SysDepartmentModel> userDeps = this.departmentService.findUserDepsByUserId(userid);
        Map<String, String> res = null;
        if (userDeps != null) {
            res = new HashMap<>(userDeps.size());
            for (SysDepartmentModel userDep : userDeps) {
                res.put(userDep.getId(), userDep.getDepName());
            }
        }
        return res;
    }
}
